package com.kye.express.business.waybill.waybill_detail;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.kye.express.R;
import com.kye.express.databinding.ItemRouterStatusBinding;
import com.kye.model.bean.logistics.RouterStatusViewType;

import java.util.List;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 20/01/2018
 * @updated : 20/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class RouterStatusTimeLineView extends LinearLayout {

    private final String TAG_DATA_ROUTERS = "TAG_DATA_ROUTERS";
//    private List<LogisticsWayBillRouterDataBean> dataList;
    private List<RouterStatusTimeLineModel> dataList;
    private LayoutInflater layoutInflater;

    public RouterStatusTimeLineView(Context context) {
        this(context, null, 0);
    }

    public RouterStatusTimeLineView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RouterStatusTimeLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.layout_router_status, this, false);
        addView(view);
    }

    public void setFinishedForView(boolean isFinished) {
        findViewById(R.id.index_content).setVisibility(!isFinished ? VISIBLE : GONE);
    }

    public void setRouterStatusDataList(List<RouterStatusTimeLineModel> dataList) {
        this.dataList = dataList;
        LinearLayout container = findViewById(R.id.content_router_status);
        container.removeAllViews();
        for (RouterStatusTimeLineModel routerData : dataList) {
            View routerStatusView = layoutInflater.inflate(R.layout.item_router_status, this, false);
            ItemRouterStatusBinding binding = DataBindingUtil.bind(routerStatusView);
            switch (routerData.getType()) {
                case TYPE_END:
                    binding.message.setTextColor(getResources().getColor(R.color.purple_0));
                    break;
                case TYPE_BEGIN:
                    break;
                case TYPE_NORMAL:
                    break;
                case TYPE_ONLY_ONE:
                    binding.message.setTextColor(getResources().getColor(R.color.purple_0));
                    break;
            }
            binding.setData(routerData);
            container.addView(routerStatusView);
        }
    }

    public static class RouterStatusTimeLineModel implements Parcelable {

        private String message;
        private String time;
        /**
         * view data
         */
        private RouterStatusViewType type;
        /**
         * 已签收，订单完成
         */
        private boolean isFinished;

        public RouterStatusTimeLineModel() {
        }

        public RouterStatusTimeLineModel(String message, String time, RouterStatusViewType type, boolean isFinished) {
            this.message = message;
            this.time = time;
            this.type = type;
            this.isFinished = isFinished;
        }

        public RouterStatusViewType getType() {
            return type;
        }

        public void setType(RouterStatusViewType type) {
            this.type = type;
        }

        public boolean isFinished() {
            return isFinished;
        }

        public void setFinished(boolean finished) {
            isFinished = finished;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }


        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.message);
            dest.writeString(this.time);
            dest.writeInt(this.type == null ? -1 : this.type.ordinal());
            dest.writeByte(this.isFinished ? (byte) 1 : (byte) 0);
        }

        protected RouterStatusTimeLineModel(Parcel in) {
            this.message = in.readString();
            this.time = in.readString();
            int tmpType = in.readInt();
            this.type = tmpType == -1 ? null : RouterStatusViewType.values()[tmpType];
            this.isFinished = in.readByte() != 0;
        }

        public static final Creator<RouterStatusTimeLineModel> CREATOR = new Creator<RouterStatusTimeLineModel>() {
            @Override
            public RouterStatusTimeLineModel createFromParcel(Parcel source) {
                return new RouterStatusTimeLineModel(source);
            }

            @Override
            public RouterStatusTimeLineModel[] newArray(int size) {
                return new RouterStatusTimeLineModel[size];
            }
        };
    }
}
